<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>
(Ver 1.0 04/09/2012 by Richard Lung<br />
Type an expression in the following box (e.g., 10.5+20*3/25).
<p>
<form method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
</p>
<ul>
<li>Only numbers and +,-,* and / operators are allowed in the expression.
<li>The evaluation follows the standard operator precedence.
<li>The calculator does not support parentheses.
<li>The calculator handles invalid input "gracefully". It does not output PHP error messages.
</ul>
Here are some(but not limit to) reasonable test cases:
<ol>
  <li> A basic arithmetic operation:  3+4*5=23 </li>
  <li> An expression with floating point or negative sign : -3.2+2*4-1/3 = 4.46666666667, 3+-2.1*2 = -1.2 </li>
  <li> Some typos inside operation (e.g. alphabetic letter): Invalid input expression 2d4+1 </li>
</ol>


<?php

$expr = $_GET["expr"];

preg_match("/[0-9-\+\*\/ ]/i", $expr, $evalexpr);

preg_match("/[a-zA-Z]/i", $expr, $checkchar);

preg_match("/[\.\+\*]{2}/i", $expr, $checkweird);

preg_match("/[^0-9-\+\*\/ ]/i", $expr, $checkmoreweird);

preg_match("/(--)+/i", $expr, $checkmoreweird2);

if(count($evalexpr) != 0 and count($checkchar) == 0 and count($checkweird)== 0 and count($checkmoreweird) == 0
			and count($checkmoreweird2) == 0) {
	
try {	
	
	eval("@\$ans = $expr ;");
	
	if(is_numeric($ans)) {
	echo "The answer to the calculated expression is = ".$ans;
	}
	else {
		echo "please enter the valid expression";
	}
}

catch (Exception $e) {
	
	echo "please enter the valid expression";
}

}

else {
	
	echo "please enter the valid expression";
}

?>

</body>
</html>